package com.jason.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jason.system.common.util.Pager;
import com.jason.system.entity.Tool;
import com.jason.system.mapper.ToolMapper;
import com.jason.system.service.ToolService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/3/26 20:17
 */
@Service
public class ToolServiceImpl implements ToolService {

  @Resource
  ToolMapper toolMapper;

  @Override
  public Tool getToolById(Integer tid) {
    if (tid==null){
      return null;
    }
    return toolMapper.getToolById(tid);
  }

  @Override
  public Pager<Tool> getAllTools(Integer page, Integer size) {
    PageHelper.startPage(page,size);
    List<Tool> tools = toolMapper.getAllTools();
    PageInfo<Tool> toolPageInfo = new PageInfo<>(tools);
    return new Pager<>(toolPageInfo);
  }

  @Override
  public Tool getToolByNumber(String number) {
    return toolMapper.getToolByNumber(number);
  }

  @Override
  public Pager<Tool> getToolByName(Integer page, Integer size, String toolName) {
    PageHelper.startPage(page,size);
    List<Tool> tools = toolMapper.getToolByName(toolName);
    PageInfo<Tool> toolPageInfo = new PageInfo<>(tools);
    return new Pager<>(toolPageInfo);
  }


  @Override
  public void insertTool(Tool tool) {
    toolMapper.insertTool(tool);
  }

  @Override
  public void updateTool(Tool tool) {
    toolMapper.updateTool(tool);
  }

  @Override
  public boolean deleteToolById(Integer tid) {
    if (tid==null){
      return false;
    }else {
      toolMapper.deleteToolById(tid);
      return true;
    }
  }

  @Override
  public boolean deleteTools(String tIds) {
    if (tIds==null||"".equals(tIds)){
      return false;
    }else {
      List<Integer> list = Arrays.stream(tIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      toolMapper.deleteTools(list);
      return true;
    }
  }
}
